package com.bc.ljn.catwars;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;

/**
 * Created by ljn on 2014/12/23.
 */
public class GameMenu {
    private Bitmap bmpMenu; //菜单背景图
    private Bitmap bmpButton, bmpButtonPressed; // 按钮图片资源
    private int btnX, btnY; // 按钮的坐标
    private Boolean isPressed; // 按钮是否按下标识位

    //菜单初始化
    public GameMenu(Bitmap bmpMenu, Bitmap bmpButton, Bitmap bmpButtonPressed) {
        this.bmpButton = bmpButton;
        this.bmpButtonPressed = bmpButtonPressed;
        this.bmpMenu = bmpMenu;

        // X居中，Y紧接屏幕底部
        btnX = GameView.screenW / 2 - bmpButton.getWidth() / 2;
        btnY = GameView.screenH * 7 / 8 - bmpButton.getHeight();
        isPressed = false;
    }

    public void draw(Canvas canvas, Paint paint) {
        //绘制菜单背景
        canvas.drawBitmap(bmpMenu, 0, 0, paint);
        //绘制未按下按钮图
        if (isPressed) {
            canvas.drawBitmap(bmpButtonPressed, btnX, btnY, paint);
        } else {
            canvas.drawBitmap(bmpButton, btnX, btnY, paint);
        }
    }

    public void onTouchEvent(MotionEvent event) {
        //获取用户当前触屏位置
        int pointX = (int) event.getX();
        int pointY = (int) event.getY();
        //当用户是按下动作或移动动作
        if (event.getAction() == MotionEvent.ACTION_DOWN ||
                event.getAction() == MotionEvent.ACTION_MOVE) {
            //判定用户是否点击了按钮
            if (pointX > btnX && pointX < (btnX + bmpButton.getWidth())) {
                if (pointY > btnY && pointY < (btnY + bmpButton.getHeight())) {
                    isPressed = true;
                } else {
                    isPressed = false;
                }
            } else {
                isPressed = false;
            }

            // 当用户是抬起动作
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            // 抬起判断是否点击按钮， 防止用户移动到别处
            if (pointX > btnX && pointX < (btnX + bmpButton.getWidth())) {
                if (pointY > btnY && pointY < (btnY + bmpButton.getHeight())) {
                    // 还原状态为未按下状态
                    isPressed = false;
                    // 改变当前游戏状态为开始游戏
                    GameView.gameState = GameView.GAMEING;
                }
            }
        }
    }
}
